import { reqCartdelete, reqCartedit, reqCartlist } from "../../http/api"
import { Toast } from "antd-mobile"
import { createSelector } from "reselect"
// 数据
const initState = {
    // 1.1购物车列表初始值
    list: [],
    // 全选
    allChecked: false

}
// 设置一个开关, 默认打开
let isRequest = true


//action types
const types = {
    // 1.2
    SHOP_CHANGE_LIST: "SHOP_CHANGE_LIST",
    SHOP_CHANGE_ALL: "SHOP_CHANGE_ALL",
}




// actions creater
export const actions = {
    // 1.4
    changeList: (list) => ({ type: types.SHOP_CHANGE_LIST, list }),
    // 1.6列表请求
    reqShopList: () => {
        return (dispatch, getState) => {
            let { info: { uid } } = getState().login
            let { list } = getState().shop
            reqCartlist({ uid }).then(res => {
                if (res.data.code === 200) {
                    let arr = res.data.list[1].content
                    arr.map(item => {
                        // 在list里面找某一项上一次的状态
                        let old = list.find(i => i.id === item.id)
                        item.checked = old ? old.checked : false
                    })
                    // arr.map(item=>item.checked=false)
                    dispatch(actions.changeList(arr))
                    // 打开开关
                    isRequest = true
                }
            })
        }
    },
       // 每一个
       checkedOne: (index) => {
        return (dispatch, getState) => {
            let { list } = getState().shop
            list[index].checked = !list[index].checked
            dispatch(actions.changeList([...list]))
            dispatch(actions.changeAll(list.every(item => item.checked)))
        }
    },
    // 全选
    changeAll: (allChecked) => ({ type: types.SHOP_CHANGE_ALL, allChecked }),
    checkedAll: () => {
        return (dispatch, getState) => {
            let { allChecked, list } = getState().shop
            let checked = !allChecked
            dispatch(actions.changeAll(checked))
            list.forEach(item => item.checked = checked);
        }
    },

    // 点击删除
    deleteShop: (id) => {
        return (dispatch, getState) => {
            reqCartdelete({ id }).then(res => {
                if (res.data.code === 200) {
                    // 弹一下成功
                    Toast.success(res.data.msg, 1)
                    // 刷新列表
                    dispatch(actions.reqShopList())
                }
            })
        }
    },
    // +
    addShop: (id) => {
        return (dispatch, getState) => {
            reqCartedit({ id, type: 2 }).then(res => {
                if (res.data.code === 200) {
                    // 刷新列表
                    dispatch(actions.reqShopList())
                }
            })
        }
    },
    // -
    subShop: (id, num) => {
        return (dispatch, getState) => {
            if (num <= 1) {
                Toast.info("宝贝不能再少了")
                return;
            }
            if (isRequest) {
                // 关闭开关
                isRequest = false
                reqCartedit({ id, type: 1 }).then(res => {
                    if (res.data.code === 200) {
                        // 刷新列表
                        dispatch(actions.reqShopList())
                    }
                })
            }
        }
    },
 
},





// reducer
export const shopReducer = (state = initState, action) => {
    switch (action.type) {
        // 修改购物车列表
        case types.SHOP_CHANGE_LIST:
            return {
                ...state,
                list: action.list
            }
            break;
        // 修改全选
        case types.SHOP_CHANGE_ALL:
            return {
                ...state,
                allChecked: action.allChecked
            }
            break;
        default:
            return state
    }
}

// 导出数据 一堆函数
// 1.5导出
export const getList = state => state.shop.list
// 导出全选
export const getAll = state => state.shop.allChecked
